<?php
namespace app\admin\controller;
/**
 * 后台会员
 */
class Activity extends Admin {

    private $_conf = array();

    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '活动管理',
                'description' => '管理网站活动',
            ),
            'menu' => array(
                array(
                    'name' => '活动列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加活动',
                    'url' => url('info'),
                ),
            ),
        );
    }
    /**
     * 列表
     */
    public function index(){
        //筛选条件
        $where = array();
        $keyword = input('keyword');
        if(!empty($keyword)){
            $where[] = ' (title like "%'.$keyword.'%")  OR ( content like "%'.$keyword.'%") ';
        }
        $groupId = input('type_id');
        if(!empty($groupId)){
            $where['type_id'] = $groupId;
        }
        //URL参数
        $pageMaps = array();
        $pageMaps['keyword'] = $keyword;
        $pageMaps['type_id'] = $groupId;
        //查询数据
        $limit=20;

        $list = model('Activity')->loadList($where,$limit);
		
		foreach( $list as $k => $v ) {
			$list[$k]['bm_num'] = db("user_sign")->where(array("activity_id" => $v['activity_id']))->count();
		}
		
        //位置导航
        $breadCrumb = array('活动列表'=>url());
        //模板传值
        $this->assign('breadCrumb',$breadCrumb);
        $this->assign('list',$list);
        $this->assign('_page',$list->render());
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }
    /**
     * 详情
     */
    public function info(){
        $activityId = input('activity_id');

        $model = model('Activity');
        if (input('post.')){
            if ($activityId){
                $status=$model->edit();

            }else{
                $status=$model->add();
                $id = $model->getLastInsID();
            }
            if($status!==false){
                $qrid = $activityId?$activityId:$id;
                $update['qrcode'] = $this->qrcode($qrid);
                $update['activity_id'] = $qrid;
                $model->save($update);
                return ajaxReturn(200,'操作成功',url('index'));
            }else{
                return ajaxReturn(0,'操作失败');
            }
        }else{
            $types = array(array('type_id'=>1,'name'=>'未成年人'),array('type_id'=>2,'name'=>'成年人'),array('type_id'=>3,'name'=>'家庭'));
            //$cates = array(array('cate_id'=>1,'name'=>'分类1'),array('cate_id'=>2,'name'=>'分类2'),array('cate_id'=>3,'name'=>'分类3'));
            $this->assign('types',$types);
            //$this->assign('cates',$cates);
            $this->assign('info', $model->getInfo($activityId));
            return $this->fetch();
        }
    }

    /**
     * 删除
     */
    public function del(){
        $activityId = input('id');
        if(empty($activityId)){
            return ajaxReturn(0,'参数不能为空');
        }
        if(model('Activity')->del($activityId)){
            return ajaxReturn(200,'活动删除成功！');
        }else{
            return ajaxReturn(0,'活动删除失败');
        }
    }
    //二维码
    public function qrcode($id){
        $config_data = db('config')->select();
        foreach( $config_data as $k => $v ) {
            $this->_conf[$v['name']] = $v['data'];
        }
        if (!$id) return "";
        $filepath = '/uploads/erweima/erweima'.$id.'.png';
        $filename = './uploads/erweima/erweima'.$id.'.png';
        if (file_exists($filename)){
            return $filepath;
        }else{
            vendor("phpqrcode.phpqrcode");
            $data = $this->_conf['site_url'] . url("home/index/event" , array("id" => $id));
            $level = 'L';
            $size = 6;
            \QRcode::png($data,$filename , $level, $size);
            return $filepath;
        }
    }
}

